package cn.itcast.orders.controller.admin;

import cn.itcast.orders.dto.OrdersCancelDTO;
import cn.itcast.orders.dto.OrdersPageQueryDTO;
import cn.itcast.orders.dto.OrdersRejectionDTO;
import cn.itcast.orders.pojo.Orders;
import cn.itcast.orders.service.OrderService;
import cn.itcast.orders.vo.OrderStatisticsVO;
import cn.itcast.result.PageBean;
import cn.itcast.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/admin/order")
@Slf4j
@Api(tags = "订单相关接口")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 订单搜索
     *
     * @param pageQueryDTO
     */
    @GetMapping("/conditionSearch")
    @ApiOperation("订单搜索")
    public Result<PageBean> conditionSearch(OrdersPageQueryDTO pageQueryDTO) {
        log.info("订单搜索：{}", pageQueryDTO);
        PageBean pageBean = orderService.conditionSearch(pageQueryDTO);
        return Result.success(pageBean);
    }

    /**
     * 各个状态的订单数量统计
     *
     * @return
     */
    @GetMapping("/statistics")
    @ApiOperation("各个状态的订单数量统计")
    public Result<OrderStatisticsVO> statistics() {
        log.info("各个状态的订单数量统计");
        OrderStatisticsVO orderStatisticsVO = orderService.statistics();
        return Result.success(orderStatisticsVO);
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @GetMapping("/details/{id}")
    @ApiOperation("查询订单详情")
    public Result<Orders> details(@PathVariable Long id) {
        log.info("查询订单详情：{}", id);
        Orders orders = orderService.getOrderDetailById(id);
        return Result.success(orders);
    }

    /**
     * 接单
     * @param map
     * @return
     */
    @PutMapping("/confirm")
    @ApiOperation("接单")
    public Result<String> confirm(@RequestBody Map<String, Long> map) {
        log.info("接单：{}", map.get("id"));
        orderService.confirm(map.get("id"));
        return Result.success();
    }


    /**
     * 拒单
     * @param ordersRejectionDTO
     * @return
     */
    @PutMapping("/rejection")
    @ApiOperation("拒单")
    public Result<String> rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) {
        log.info("拒单：{}", ordersRejectionDTO);
        orderService.rejection(ordersRejectionDTO);
        return Result.success();
    }


    /**
     * 取消订单
     * @param ordersCancelDTO
     * @return
     */
    @PutMapping("/cancel")
    @ApiOperation("取消订单")
    public Result<String> cancelOrder(@RequestBody OrdersCancelDTO ordersCancelDTO) {
        log.info("取消订单：{}",ordersCancelDTO);
        orderService.cancelOrder(ordersCancelDTO, true);
        return Result.success();
    }


    /**
     * 派送订单
     * @param id
     * @return
     */
    @PutMapping("/delivery/{id}")
    @ApiOperation("派送订单")
    public Result<String> delivery(@PathVariable Long id) {
        log.info("派送订单：{}", id);
        orderService.delivery(id);
        return Result.success();
    }

    /**
     * 完成订单
     * @param id
     * @return
     */
    @PutMapping("/complete/{id}")
    @ApiOperation("完成订单")
    public Result<String> complete(@PathVariable Long id) {
        log.info("完成订单：{}", id);
        orderService.complete(id);
        return Result.success();
    }

}
